import os
import sys
from tkinter import Label
import art
import cv2
import 游戏页面
import pygame
from XiaoXiaoLe import *
from XiaoXiaoLe.游戏主程序 import pacerGame
from tkinter import Tk
from PIL import ImageTk, Image
import pyttsx3 as pyttsx

'''游戏开始'''
def kai():

    # 初始化Pygame
    pygame.init()

    # 设置窗口大小
    screen = pygame.display.set_mode((600, 600))

    # 设置窗口标题
    pygame.display.set_caption("爱情公寓消消乐")

    # 加载游戏开始界面背景图片
    blackgroup0 = pygame.image.load("res/imgs/pacer1.png")
    blackgroup1 = pygame.image.load("res/imgs/game_background.png")
    blackgroup2 = pygame.image.load("res/imgs/pacer2.png")
    blackgroup3 = pygame.image.load("res/imgs/pacer3.png")
    blackgroup4 = pygame.image.load("res/imgs/pacer4.png")
    blackgroup5 = pygame.image.load("res/imgs/pacer5.png")
    blackgroup6 = pygame.image.load("res/imgs/pacer6.png")
    blackgroup7 = pygame.image.load("res/imgs/pacer7.png")
    blackgroup8 = pygame.image.load("res/imgs/img.png")
    map0 = pygame.transform.scale(blackgroup0, (200, 200))
    map1 = pygame.transform.scale(blackgroup1, (200, 200))
    map2 = pygame.transform.scale(blackgroup2, (200, 200))
    map3 = pygame.transform.scale(blackgroup3, (200, 200))
    map4 = pygame.transform.scale(blackgroup4, (200, 200))
    map5 = pygame.transform.scale(blackgroup5, (200, 200))
    map6 = pygame.transform.scale(blackgroup6, (200, 200))
    map7 = pygame.transform.scale(blackgroup7, (200, 200))
    map8 = pygame.transform.scale(blackgroup8, (200, 200))


    # 设置字体和文本大小
    font1 = pygame.font.Font(None, 36)
    font2 = pygame.font.Font(None, 20)
    font3 = pygame.font.Font(None, 70)
    font4 = pygame.font.Font(None, 36)

    # 创建文本对象
    text1 = font1.render("PRESS Q ENTER GAME ", True, (255, 255, 255))
    text2 = font2.render("The game was made by Chen Yue.", True, (0, 0, 0))
    text3 = font3.render("Love Apartment Chilling", True, (255, 100, 0))
    text4 = font4.render("PRESS ESC EXIT GAME ", True, (255, 255, 255))

    # 设置文本位置
    text_rect1 = text1.get_rect()
    text_rect1.centerx = screen.get_width() / 2
    text_rect1.centery = screen.get_height() / 2+200
    text_rect2 = text3.get_rect()
    text_rect2.centerx = screen.get_width() / 2
    text_rect2.centery = screen.get_height() / 2
    text_rect3 = text4.get_rect()
    text_rect3.centerx = screen.get_width() / 2
    text_rect3.centery = screen.get_height() / 2 +250


    # 游戏开始选择开始或退出游戏循环
    flag=False
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):
                engine = pyttsx.init()
                engine.say('Exit the game')
                engine.runAndWait()
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYUP and event.key == pygame.K_q:
                flag = True
        if flag:
            engine = pyttsx.init()
            engine.say('Game start')
            engine.runAndWait()
            break

        # 背景图的位置
        screen.blit(map0, (0, 0))
        screen.blit(map1, (200, 200))
        screen.blit(map2, (400, 400))
        screen.blit(map3, (200, 0))
        screen.blit(map4, (0, 200))
        screen.blit(map5, (0, 400))
        screen.blit(map6, (200, 400))
        screen.blit(map7, (400, 200))
        screen.blit(map8, (400, 0))
        # 在背景上绘制文本
        screen.blit(text1, text_rect1)
        screen.blit(text2, (10, 10))
        screen.blit(text3, text_rect2)
        screen.blit(text4, text_rect3)
        pygame.display.flip()  # 更新屏幕

def main(game=None):
    pygame.init()
    screen = pygame.display.set_mode(游戏页面.SCREENSIZE)
    pygame.display.set_caption('爱情公寓开心消消乐')
    # 加载背景音乐
    pygame.mixer.init()
    pygame.mixer.music.load(os.path.join(游戏页面.ROOTDIR, "res/audios/爱情公寓全体 - 我的未来式.mp3"))
    pygame.mixer.music.set_volume(0.6)
    pygame.mixer.music.play(-1)
    # 加载音效，消除音效
    sounds = {}
    sounds['mismatch'] = pygame.mixer.Sound(os.path.join(游戏页面.ROOTDIR, 'res/audios/badswap.wav'))
    sounds['match'] = []
    for i in range(6):
        sounds['match'].append(pygame.mixer.Sound(os.path.join(游戏页面.ROOTDIR, 'res/audios/match%s.wav' % i)))

    # 字体显示
    font = pygame.font.Font(os.path.join(游戏页面.ROOTDIR, 'res/font/font.TTF'), 25)
    # 星星
    pacer_imgs = []
    for i in range(1, 8):
        pacer_imgs.append(os.path.join(游戏页面.ROOTDIR, 'res/imgs/pacer%s.png' % i))
    # 循环
    game = pacerGame(screen, sounds, font, pacer_imgs, 游戏页面)
    while True:
        score = game.start()
        flag = False
        # 游戏时间结束后给出选择，玩家选择重玩或者退出
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):
                    engine = pyttsx.init()
                    engine.say('Game over')
                    engine.runAndWait()
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.KEYUP and event.key == pygame.K_r:
                    flag = True
            if flag:
                engine = pyttsx.init()
                engine.say('Restart the game')
                engine.runAndWait()
                break
            screen.fill((136, 207, 236))
            text0 = 'Final score: %s' % score
            text1 = 'Press <R> to restart the game.'
            text2 = 'Press <Esc> to quit the game.'
            y = 150
            for idx, text in enumerate([text0, text1, text2]):
                text_render = font.render(text, 1, (85, 65, 0))
                rect = text_render.get_rect()
                if idx == 0:
                    # rect.left, rect.top = (223, y)
                    rect.left, rect.top = (200, y)
                elif idx == 1:
                    rect.left, rect.top = (96.5, y)
                else:
                    rect.left, rect.top = (93.5, y)
                y += 99
                screen.blit(text_render, rect)
            pygame.display.update()
        game.reset()


'''游戏运行'''
if __name__ == '__main__':
    kai()
    main()